package com.ly.lypopupwindowdemo.util;


import android.content.Context;
import android.os.Environment;

import com.ly.lypopupwindowdemo.MyApplication;

import java.io.File;

/**
 * 目录工具
 * Created by Yhyu on 2015/5/29.
 */
public class DirUtil {
    public static final String IMAGE_CACHE_DIR = "ImageCache";
    public static final String CRASH_DIR = "crash";
    public static final String IMAGE_DIR = "Download";
    public static final String FILE_DIR = "filecache";

    public static String getImageCachePath() {
        String dir =getDiskCacheDir(MyApplication.getInstance(), IMAGE_CACHE_DIR);
        FileUtil.mkDirs(dir);
        return dir;
    }

    public static String getAppCrashPath() {
        String dir =getDiskCacheDir(MyApplication.getInstance(), CRASH_DIR);
        FileUtil.mkDirs(dir);
        return dir;
    }

    public static String getAppImagePath() {
        String dir = getDiskCacheDir(MyApplication.getInstance(), IMAGE_DIR);
        FileUtil.mkDirs(dir);
        return dir;
    }

    public static String getAppFileCachePath() {
        String dir = getDiskCacheDir(MyApplication.getInstance(), FILE_DIR);
        FileUtil.mkDirs(dir);
        return dir;
    }


    public static String getDiskCacheDir(Context context, String dirName) {
        String cachePath = null;
        File cacheDir;
        if ("mounted".equals(Environment.getExternalStorageState())) {
            cacheDir = context.getExternalCacheDir();
            if (cacheDir != null) {
                cachePath = cacheDir.getPath();
            }
        }

        if (cachePath == null) {
            cacheDir = context.getCacheDir();
            if (cacheDir != null && cacheDir.exists()) {
                cachePath = cacheDir.getPath();
            }
        }

        return cachePath + File.separator + dirName;
    }
}
